define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/info/index' + location.search,
                    add_url: 'user/info/add/ids/' + Config.user_id,
                    // edit_url: 'user/info/edit',
                    del_url: 'user/info/del',
                    multi_url: 'user/info/multi',
                    import_url: 'user/info/import',
                    table: 'user_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.user_id = Fast.api.query('ids');
                    op.id = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'first_cate', title: __('一级分类'), searchList: Config.categoryList, formatter: Table.api.formatter.flag },
                        { field: 'two_cate', title: __('二级分类'), searchList: Config.categoryList, formatter: Table.api.formatter.flag },
                        { field: 'user.username', title: __('User.username'), operate: 'LIKE' },
                        { field: 'group.name', title: __('Group.name'), operate: 'LIKE' },
                        { field: 'shareholder', title: __('联创&分红股东'), operate: false, searchList: Config.groupList, formatter: Table.api.formatter.flag },
                        { field: 'investment', title: __('创投'), operate: false, searchList: Config.groupList, formatter: Table.api.formatter.flag },
                        { field: 'images', title: __('授权合同'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images },
                        { field: 'address', title: __('Address'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'expirationtime', title: __('Expirationtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'setgroup',
                                    title: '设置身份',
                                    text: '设置身份',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'user/info/setgroup',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.group_id == 3) {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'settlement',
                                    title: '业绩结算',
                                    text: '业绩结算',
                                    extend: 'data-area=["90%","90%"]',
                                    // classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                    confirm: '是否更新结算时间？',
                                    url: 'user/info/settlement',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.shareholder > 3 || row.investment == 6) {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },

                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        settlement: function () {
            Controller.api.bindevent();
        },
        setgroup: function () {
            Controller.api.bindevent();
        },
        add: function () {
            $("#c-address").on("cp:updated", function () {
                var citypicker = $(this).data("citypicker");
                var value = citypicker.getVal();

            });

            // $("#c-goods_id").data("params", function (obj) {
            //     var address = $("#c-address").val();
            //     var first_cate = $(".first_cate").val();
            //     var two_cate = $(".two_cate").val();
            //     if (address.split('/').length == 3) {
            //         if ($("#c-address").val() != undefined) {
            //             var address = $("#c-address").val();
            //         }
            //     }
            //     return { custom: { address: address ?? '', first_cate: first_cate ?? 0, two_cate: two_cate ?? 0 } };
            // })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
